<?php

namespace App\Services\Tencent;

use App\Models\ImFilesStorage;
use Qcloud;

class UploadServices extends BaseService
{
    /**
     *文件上传(图片/视频)
     * @param $folder  路径加文件名
     * @param $realPath  是现文件的路径
     * $power //私有1, 公共读2,公共读写3
     *author:tangfen
     *date:$ct
     */

    public function uploadImg($file, $folder, $power = '')
    {
       // 上传到七牛后保存的文件名
        $fileName = $folder.'/'.date('Ymd') . '/' . date('YmdHis') . uniqid() . '.'.$file->getClientOriginalExtension();

        if ($power == 1) {//私有桶
            $bucket = $this->privateBucket;
        } else {//公开桶
            $bucket = $this->publicBucket;
        }
        $cosClient = new Qcloud\Cos\Client(array('region' => $this->region,
            'credentials' => array(
                'appId' => $this->appid,
                'secretId' => $this->secretId,
                'secretKey' => $this->secretKey)));
        try {
            $result = $cosClient->putObject(array(
                'Bucket' => $bucket,
                'Key' => $fileName,
                'Body' => fopen($file, 'rb'),
                'ServerSideEncryption' => 'AES256'));
            return $result;
        } catch (\Exception $e) {
            return 1;  //echo "$e\n";
        }
    }

    /**
     *删除文件
     * @param $file_id
     *author:tangfen
     *date:$ct
     */
    public function delFile($file)
    {
        $path_file = explode('com/',$file);
        //数据入库
        $ImFilesStorage = new ImFilesStorage();
        $power = $ImFilesStorage->where('link_url', $file)->value('power'); //私有1, 公共读2,公共读写3
        if ($power != 1) {
            $bucket = $this->publicBucket; //公开桶
        } else {
            $bucket = $this->privateBucket; //私有桶
        }
        $cosClient = new Qcloud\Cos\Client(
            array(
                'region' => $this->region,
                'schema' => 'https', //协议头部，默认为http
                'credentials' => array(
                    'secretId' => $this->secretId,
                    'secretKey' => $this->secretKey)));
        try {
            $cosClient->deleteObject(array(
                'Bucket' => $bucket, //存储桶名称，由BucketName-Appid 组成，可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucket
                'Key' => $path_file[1]
            ));
            //软删数据库状态
            $file = ImFilesStorage::where('link_url', $file)->first();
            $file->status = 3;  //3已删除
            $file->save();
            return 2;//成功
        } catch (\Exception $e) {
            return 1;//请求失败  echo($e);
        }
    }


    /**
     * 获取私有文件的访问凭证 返回可以访问的URL  在有效期内可以访问
     * @param $url tangfen/20211207/2021120716241961af1a33e9e1f.jpg
     * @param string $expires +3000 minutes
     *author:tangfen
     *date:$ct
     */
    public function getObjectUrl($url, $expires = '+3000 minutes')
    {
        $cosClient = new Qcloud\Cos\Client(
            array(
                'region' => 'ap-guangzhou',
                'schema' => 'https', //协议头部，默认为http
                'credentials' => array(
                    'secretId' => $this->secretId,
                    'secretKey' => $this->secretKey)));
        try {
            $bucket = $this->privateBucket; //存储桶，存储桶名称，由BucketName-Appid 组成，可以在COS控制台查看 https://console.cloud.tencent.com/cos5/bucket
            $key = $url; //对象在存储桶中的位置，即对象键
            $signedUrl = $cosClient->getObjectUrl(
                $bucket,
                $key,
                $expires //签名的有效时间
            );
            // 请求成功
            return $signedUrl;
        } catch (\Exception $e) {
            // 请求失败
            print_r($e);
        }
    }

}
